package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.service.impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

import static com.itheima.reggie.common.R.success;

/**
 * @author tongchen
 * @create 2023-01-03 11:01
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.进行密码加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据用户名查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        //对结果进行判断
        if(emp==null){
            return R.error("不存在该用户");
        }
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        //判断该用户是否禁用
        if(emp.getStatus()==0){
            return R.error("该用户已经禁用");
        }
        //将session数据放入缓存
        request.getSession().setAttribute("employee",emp.getId());
        return success(emp);

    }
    @PostMapping("/logout")
    public R<String> logOut(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return success("退出成功");
    }
    @PostMapping()
    public R<String> addEmp(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        //加入密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置时间和人
       /* employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long emplpyee1 =(Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(emplpyee1);
        employee.setUpdateUser(emplpyee1);*/
        employeeService.save(employee);
        return success("保存成功");
    }
    @GetMapping("/page")
    public R<Page> getPage(int page,int pageSize,String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        //创建分页构造器
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(!StringUtils.isEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return success(pageInfo);
    }
    @PutMapping()
    public R<String> updateStatus(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        //更新修改时间和作者
        Long empId =(Long) request.getSession().getAttribute("employee");
        //通过前端传来的数据进行修改操作
        boolean b = employeeService.updateById(employee);
        return R.success("修改信息成功");
    }
    @GetMapping("/{id}")
    public R<Employee>updateEmployee(@PathVariable Long id){
        log.info("根据id查询信息，为后续修改做铺垫");
        //获取数据
        Employee byId = employeeService.getById(id);
        if(byId==null){
          return  R.error("查无此人");
        }else {
             return R.success(byId);
        }

    }
}
